package com.rockwell.sniffyhunter.model;

import com.rockwell.sniffyhunter.utils.IRobotSensors;

public class RobotPosition {
	public RobotJunctionLocation junctionLocation;
	public short laneLocation;
	public double orientation;	// the orientation of the robot read from the Compass
	public boolean frontwalldetected;
	public boolean front1walldetected;
	public boolean front2walldetected;
	public boolean front3walldetected;
	public boolean leftwalldetected;
	public boolean rightwalldetected;
	public boolean leftwall2detected;
	public boolean rightwall2detected;
	public boolean wheelMoving;
	
	public RobotPosition(boolean front, boolean leftWallPresent, boolean rightWallPresent) {
		this.junctionLocation = RobotJunctionLocation.InitialPosition;
		this.frontwalldetected = front;
		this.orientation = 0;
		this.leftwalldetected = leftWallPresent;
		this.rightwalldetected = rightWallPresent;
		this.laneLocation = 0;
		wheelMoving = false;
	}
	
	public RobotPosition(int flags) {
		frontwalldetected = (flags & IRobotSensors.FRONT_SENSOR_MASK) != 0;
		front1walldetected = (flags & IRobotSensors.FRONT_SENSOR1_MASK) != 0;
		front2walldetected = (flags & IRobotSensors.FRONT_SENSOR2_MASK) != 0;
		front3walldetected = (flags & IRobotSensors.FRONT_SENSOR3_MASK) != 0;
		leftwalldetected = (flags & IRobotSensors.LEFT_SENSOR1_MASK) != 0;
		rightwalldetected = (flags & IRobotSensors.RIGHT_SENSOR1_MASK) != 0;
		leftwall2detected = (flags & IRobotSensors.LEFT_SENSOR2_MASK) != 0;
		rightwall2detected = (flags & IRobotSensors.RIGHT_SENSOR2_MASK) != 0;
		wheelMoving = (flags & IRobotSensors.WHEEL_STATE_MASK) != 0;
		
		this.junctionLocation = RobotJunctionLocation.InitialPosition;
		this.orientation = 0;
		this.laneLocation = 0;
	}
	
	public boolean isTooLeft() {
		return (laneLocation & RobotLaneLocation.TooLeft) != 0;
	}
	
	public boolean isLeft() {
		return (laneLocation & RobotLaneLocation.Left) != 0;
	}
	
	public boolean isTooRight() {
		return (laneLocation & RobotLaneLocation.TooRight) != 0;
	}
	
	public boolean isRight() {
		return (laneLocation & RobotLaneLocation.Right) != 0;
	}
	
	public boolean isCenter() {
		return laneLocation == RobotLaneLocation.Center;
	}
	
	public boolean isFrontTooClose() {
		return (laneLocation & RobotLaneLocation.FrontTooClose) != 0;
	}
	
	public boolean isFrontClose() {
		return (laneLocation & RobotLaneLocation.FrontClose) != 0;
	}

	public boolean isFrontCloseOrTooClose() {
		return ((laneLocation & RobotLaneLocation.FrontClose) != 0 ||
				(laneLocation & RobotLaneLocation.FrontTooClose) != 0);
	}
}
